package com.marketingexpert.controller.marketingmgnt.autoMarketing;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.omg.CORBA.UserException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.marketingexpert.domain.User;
import com.marketingexpert.domain.UserFuncEnable;
import com.marketingexpert.domain.UserFuncEnableWeiboId;
import com.marketingexpert.exception.MarketingExpertException;
import com.marketingexpert.service.marketingmgnt.MarketingService;
import com.marketingexpert.service.usermgnt.UserService;

public class LoadExistEnableFuncController implements Controller {
	
	private static Logger logger = Logger.getLogger(LoadExistEnableFuncController.class);
	
	private MarketingService marketingService;
	
	private UserService userService;
	
	@Override
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		response.setContentType("application/x-json;charset=GBK"); 
		response.setHeader("Pragma", "No-cache"); 
		response.setHeader("Cache-Control", "no-cache"); 
		response.setHeader("Cache-Control", "no-store"); 
		response.setDateHeader("Expires", 0);  
		PrintWriter out = response.getWriter();
		
		String parentUserId = (String) request.getSession().getAttribute("parentUserId");
		try {
			User user = getUserService().queryUserByUId(parentUserId);
		
			if(user != null) {
				String funcCode = request.getParameter("funcCode");
				UserFuncEnable ufe = marketingService.queryEnabledFunc(funcCode, user.getId());
				if(ufe == null) {
					return null;
				}
				JSONObject _result = JSONObject.fromObject(ufe); 
				out.print(_result);
				return null;
			}
			return null;
		} catch(MarketingExpertException e) {
			logger.error(e.getMessage(), e);
			return null;
		}
		
	}

	public MarketingService getMarketingService() {
		return marketingService;
	}

	public void setMarketingService(MarketingService marketingService) {
		this.marketingService = marketingService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}
